/*
 * @Author: caozaihui
 * @Date: 2024-06-21 09:36:08
 * @LastEditors: czh 2119294696@qq.com
 * @LastEditTime: 2024-06-29 21:23:12
 * @FilePath: \YMU_forum_vue\src\utils\http.js
 */
import axios from 'axios';
import { ElMessage } from 'element-plus'
const httpInstance = axios.create({
    baseURL: 'http://127.0.0.1:8502/',
    timeout: 5000
});

httpInstance.interceptors.response.use(res => {
    if (res.status == 200) {
        return res.data;
    }  else {
        ElMessage.error(res.data.msg);
    }
}, e => {
    return Promise.reject('后端错误。。');
});

httpInstance.interceptors.request.use(
    //请求头中携带token叫做Authorization
    //我草这个逼玩意只能使用cookie，不能使用pinia中的，坑死我劳资了
    config => {
        const token = localStorage.getItem("token");
        if(token){
            config.headers.Authorization = token;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

export default httpInstance;